package com.smarttable.ysd.floatwindow;

import wei.mark.standout.StandOutWindow;
import wei.mark.standout.constants.StandOutFlags;
import wei.mark.standout.ui.Window;
import android.media.AudioManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.smarttable.ysd.R;

public class MuteWindow2 extends StandOutWindow {

	private ImageView mSoundImage;
	@Override
	public String getAppName() {
		return "Mute";
	}

	@Override
	public int getAppIcon() {
		return android.R.drawable.ic_menu_close_clear_cancel;
	}

	@Override
	public void createAndAttachView(int id, FrameLayout frame) {
		// create a new layout from body.xml
		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.mute_window, frame, true);
		mSoundImage = (ImageView)frame.findViewById(R.id.sound);
	}

	// the window will be centered
	@Override
	public StandOutLayoutParams getParams(int id, Window window) {
		return new StandOutLayoutParams(id, 116, 116,
				StandOutLayoutParams.LEFT, StandOutLayoutParams.TOP);
	}

	// move the window by dragging the view
	@Override
	public int getFlags(int id) {
		return super.getFlags(id) | StandOutFlags.FLAG_BODY_MOVE_ENABLE
				| StandOutFlags.FLAG_WINDOW_FOCUSABLE_DISABLE;
	}


	public void jingyin(View view){
		toggleRingMode();
		setSoundImage();
	}
	
	private void setSoundImage() {
	    AudioManager am = (AudioManager) this.getSystemService(AUDIO_SERVICE);
	    boolean mute = am.getStreamVolume(AudioManager.STREAM_MUSIC) == 0;
	    int imageResource = mute ? R.drawable.sound_mute : R.drawable.sound_nor;
	    this.mSoundImage.setImageResource(imageResource);
	}
	
	private void toggleRingMode() {
	    AudioManager am = (AudioManager) this.getSystemService(AUDIO_SERVICE);
	    boolean mute = am.getStreamVolume(AudioManager.STREAM_MUSIC) == 0;
	    if(mute){
//	    	am.setStreamMute(AudioManager.STREAM_MUSIC, false);
//	    	am.setStreamMute(AudioManager.STREAM_ALARM, false);
//	    	am.setStreamMute(AudioManager.STREAM_DTMF, false);
//	    	am.setStreamMute(AudioManager.STREAM_RING, false);
//	    	am.setStreamMute(AudioManager.STREAM_SYSTEM, false);
//	    	am.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
//	    	am.setStreamMute(AudioManager.STREAM_VOICE_CALL, false);
			am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
	    }else{
//	    	am.setStreamMute(AudioManager.STREAM_MUSIC, true);
//	    	am.setStreamMute(AudioManager.STREAM_ALARM, true);
//	    	am.setStreamMute(AudioManager.STREAM_DTMF, true);
//	    	am.setStreamMute(AudioManager.STREAM_RING, true);
//	    	am.setStreamMute(AudioManager.STREAM_SYSTEM, true);
//	    	am.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
//	    	am.setStreamMute(AudioManager.STREAM_VOICE_CALL, true);
			am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
	    }
	}
}
